﻿using DevExpress.XtraEditors;
using HZH_Controls.Controls;
using JZ_UDI.DataAccess.DataEntity;
using JZ_UDI.Settings;
using JZ_UDI.View.Settings;
using NLog;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using ComboBox = System.Windows.Forms.ComboBox;

namespace JZ_UDI
{
    public partial class frmMain: Form
    {

        private Form activeForm;

        public frmMain()
        {
            InitializeComponent();

            lblLogoTitle.Font = new Font(Application.StartupPath + "\\Assets\\font\\AlimamaShuHeiTi-Bold", 18F,FontStyle.Bold);
            this.DoubleBuffered = true; // 可以减少拖动时的闪烁  

            this.MaximizedBounds = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
        }

        private Point mPoint;


        private void btnMin_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void btnMax_Click(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)
            {
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
                this.WindowState = FormWindowState.Maximized;
                this.MaximizedBounds = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
            Global.SaveLogger(LogLevel.Info, Global.logger.Name, "关闭系统");
            Application.Exit();
        }

        private void panelTitle_MouseDown(object sender, MouseEventArgs e)
        {
            mPoint = new Point(e.X, e.Y);
        }

        private void panelTitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
            }
        }

        private void panelTitle_DoubleClick(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)
            {
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
                this.WindowState = FormWindowState.Maximized;
                this.MaximizedBounds = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
            }
        }

        private void btnSettingUDI_Click(object sender, EventArgs e)
        {
            frmUDI frm1 = new frmUDI();
            frm1.Text = "UDI数据设置";
            frm1.MdiParent=this;
            frm1.Show();
        }

        private void lblPassword_MouseHover(object sender, EventArgs e)
        {
            lblPassword.Font = new Font(lblPassword.Font, FontStyle.Underline);
            lblPassword.Cursor = Cursors.Hand;
            lblPassword.ForeColor = Color.Red;
        }

        private void lblPassword_MouseLeave(object sender, EventArgs e)
        {
            lblPassword.Font = new Font(lblPassword.Font, FontStyle.Regular);
            lblPassword.Cursor = Cursors.Default;
            lblPassword.ForeColor = Color.White;
        }

        private void lblExit_MouseHover(object sender, EventArgs e)
        {
            lblExit.Font = new Font(lblPassword.Font, FontStyle.Underline);
            lblExit.Cursor = Cursors.Hand;
            lblExit.ForeColor = Color.Red;
        }

        private void lblExit_MouseLeave(object sender, EventArgs e)
        {
            lblExit.Font = new Font(lblPassword.Font, FontStyle.Regular);
            lblExit.Cursor = Cursors.Default;
            lblExit.ForeColor = Color.White;
        }

        private void lblPassword_Click(object sender, EventArgs e)
        {
            frmChangPwd frmpwd = new frmChangPwd();
            frmpwd.ShowDialog();
        }


        private void OpenChildForm(Form childForm,string modelName)
        {

            int pID = Global.PriList.Find(o => o.PrivilegeItem.Equals(modelName)).ID;
            UserPrivilege up = Global.UserPrivileges.Find(o => o.PrivilegeID == pID);

            if (up == null)
            {
                MessageBox.Show("权限不足", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (activeForm != null)
            {
                activeForm.Close();
            }
            activeForm = childForm;
            childForm.TopLevel = false;
            childForm.FormBorderStyle = FormBorderStyle.None;
            childForm.Dock = DockStyle.Fill;
            this.panelDesktop.Controls.Add(childForm);
            this.panelDesktop.Tag = childForm;
            childForm.BringToFront();
            childForm.Show();

        }

        private void uiNavMenu1_MenuItemClick(TreeNode node, Sunny.UI.NavMenuItem item, int pageIndex)
        {
            switch (node.Name)
            {
                case "btnSetUdi":   //设置UDI
                    {
                        OpenChildForm(new frmUDI(),"UDI系统");
                        Global.SaveLogger(LogLevel.Info, Global.logger.Name, "用户进入设置-UDI参数");
                        break;
                    }
                case "btnMainPage": //主界面
                    {
                        //OpenChildForm(new frmMainPage());
                        //Global.SaveLogger(LogLevel.Info, Global.logger.Name, "用户进入主界面");
                        break;
                    }
                case "btnVendorCode":   //厂商代码
                    {
                        OpenChildForm(new frmVendorCode(),"厂商代码");
                        Global.SaveLogger(LogLevel.Info, Global.logger.Name, "用户进入厂商代码界面");
                        break;
                    }
                case "btnAccountManagement":
                    {
                        OpenChildForm(new frmAccountManagement(),"帐号管理");
                        Global.SaveLogger(LogLevel.Info, Global.logger.Name, "用户进入帐号管理界面");
                        break;
                    }
            }


        }

        private void lblExit_Click(object sender, EventArgs e)
        {
            Global.SaveLogger(LogLevel.Info, Global.logger.Name, "用户退出");
            Application.Exit();
        }
    }
}
